package com.wandian.executor;

import java.util.concurrent.ThreadFactory;

import com.wandian.util.Log;

/**
 * @author long
 * @Create 2012-10-10 上午11:07:11
 * @QQ 108232706
 * @version 1.0
 * @description SimpleThreadFactory
 */
public class SimpleThreadFactory implements ThreadFactory, Thread.UncaughtExceptionHandler {
	private final boolean daemon;
	private final String threadName;

	public SimpleThreadFactory(String threadName, boolean daemon) {
		this.threadName = threadName;
		this.daemon = daemon;
	}

	public Thread newThread(Runnable r) {
		Thread t = new Thread(r, this.threadName + TimerMgr.getTimerIndex());
		t.setDaemon(this.daemon);
		t.setUncaughtExceptionHandler(this);
		return t;
	}

	public void uncaughtException(Thread thread, Throwable throwable) {
		Log.error("Uncaught Exception in thread " + thread.getName(), throwable);
	}
}